#include "pinctrl.h"
#include "uart.h"
#include "osal_debug.h"
#include "soc_osal.h"
#include "app_init.h"
#include "string.h"
#include "cmsis_os2.h"
#include "pinctrl.h"
#include "gpio.h"
#include "soc_osal.h"
#include "app_init.h"
#include "cmsis_os2.h"
#include "platform_core_rom.h"
#include "UART2.h"


#define DELAY_TIME_MS 1
#define UART_RECV_SIZE 10

uint8_t uart2_recv[UART_RECV_SIZE] = {0};
uint8_t recv1[11]={0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19};


#define UART_INT_MODE 1
#if (UART_INT_MODE)

#endif


typedef struct {
    char *ATSendStr; // 发送指令
    char *ATRecStr;  // 回复指令
} EC800_ATCmds;

EC800_ATCmds AT_Call_Phone[] = {
    {"ATD13189941518;\r","OK\r\n"}//电话功能

};



uart_buffer_config_t g_app_uart2_buffer_config = {.rx_buffer = uart2_recv, .rx_buffer_size = UART_RECV_SIZE};
// 初始化 UART 引脚
void uart2_gpio_init(void)
{

    uapi_pin_set_mode(GPIO_07 , PIN_MODE_2);
    uapi_pin_set_mode(GPIO_08, PIN_MODE_2);


}
// 初始化 UART 配置
void uart2_init_config(void)
{
    // 定义一个 uart_attr_t 类型的结构体变量 attr，用于存储 UART 的属性
    uart_attr_t attr = {
        .baud_rate = 115200, .data_bits = UART_DATA_BIT_8, .stop_bits = UART_STOP_BIT_1, .parity = UART_PARITY_NONE};

    uart_pin_config_t pin_config = {.tx_pin = GPIO_08, .rx_pin = GPIO_07, .cts_pin = PIN_NONE, .rts_pin = PIN_NONE};
    uapi_uart_deinit(UART_BUS_2);
    int ret = uapi_uart_init(UART_BUS_2, &pin_config, &attr, NULL, &g_app_uart2_buffer_config);
    if (ret != 0) {
        printf("uart init failed ret = %02x\n", ret);
    }
}

#if (UART_INT_MODE)
void uart2_read_handler(const void *buffer, uint16_t length, bool error)
{
    unused(error);
    if (buffer == NULL || length == 0) {
        return;
    }
    if (memcpy_s(uart2_recv, length, buffer, length) != EOK) {
        return;
    }
    
    
}
#endif

uint32_t uart2_send_buff(uint8_t *str, uint16_t len)
{
    uint32_t ret = 0;
    // printf("[AT SEND ]: %s\n", str);
    ret = uapi_uart_write(UART_BUS_2, str, len, 0xffffffff);
    if (ret != 0) {
        return ret;
    }
    return ret;
}

void ec800_Call_Phone(void)
{
    
    uart2_send_buff((uint8_t *)AT_Call_Phone[0].ATSendStr, strlen(AT_Call_Phone[0].ATSendStr));
     if (strstr((char *)uart2_recv, AT_Call_Phone[0].ATRecStr) != NULL) {//通过字符串匹配判断是否包含预期响应（如OK\r\n）,即使接通也会打印失败
                printf(" %s call phone succeed !!!\n", AT_Call_Phone[0].ATSendStr);
                
                
            } else {
                printf(" %s call phone fail !!!\n", AT_Call_Phone[0].ATSendStr);
                
            }
            memset(uart2_recv, 0, sizeof(uart2_recv));
}



void uart2_init(void)
{
     // 对 UART 相关的 GPIO 进行初始化
    uart2_gpio_init();
    // 对 UART 进行初始化配置
    uart2_init_config();
    
    // 如果定义了 UART_INT_MODE 宏，即使用中断模式

    // 注册串口回调函数
   // if (uapi_uart_register_rx_callback(0, UART_RX_CONDITION_MASK_IDLE, 1, uart_read_handler) == ERRCODE_SUCC) {
    if (uapi_uart_register_rx_callback(2, UART_RX_CONDITION_MASK_IDLE, 1, uart2_read_handler) == ERRCODE_SUCC) {
    }
    

    
    



}
